package com.ljye.jg.ljye.activity;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ljye.jg.ljye.R;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Activity_Punch_Sign extends Activity {

    private static final int ON_REFRESH = 1;
    private static final int ON_LOAD_MORE = 2;
    private final DateFormat FORMATTER = SimpleDateFormat.getDateInstance();
    public View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.back://返回
                    finish();
                    break;

                default:
                    break;
            }
        }
    };
    private ImageView back;//返回
    private MaterialCalendarView widget;//日历
    private TextView textView;//测试
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_punch_sign);
        initView();
        //初始化日历
        initDate();
        back.setOnClickListener(listener);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                widget.setSelectedDate(Calendar.getInstance().getTime());
            }
        }, 900);

    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        widget = (MaterialCalendarView) findViewById(R.id.calendarView);
        textView = (TextView) findViewById(R.id.text);//测试

    }

    //日期显示
    private String getSelectedDatesString() {
        CalendarDay date = widget.getSelectedDate();
        if (date == null) {
            return " ";
        }
        return FORMATTER.format(date.getDate());
    }

    //初始化日历
    private void initDate() {
        textView.setText(getSelectedDatesString());
        //箭头颜色
        widget.setArrowColor(getResources().getColor(R.color.activity_title_color));
        //选择的颜色
        widget.setSelectionColor(getResources().getColor(R.color.activity_title_color));
        widget.setTileHeight(100);
//        widget.setTileHeightDp(40);
        //日期文字大小
//        widget.setDateTextAppearance(18);
        widget.setDateTextAppearance(R.style.TextDate);
        //星期文字大小
        widget.setWeekDayTextAppearance(16);
        //周一开头
        widget.state().edit()
                .setFirstDayOfWeek(Calendar.MONDAY)
                .commit();
        //选择模式
        widget.setSelectionMode(MaterialCalendarView.SELECTION_MODE_NONE);
        //设置日期  月份0-11
//            widget.setSelectedDate(CalendarDay.from(2016, 11, 2));
        //系统时间
//        widget.setSelectedDate(Calendar.getInstance().getTime());

//        //日期变化
//        widget.setOnDateChangedListener(new OnDateSelectedListener() {
//            @Override
//            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
//                textView.setText(getSelectedDatesString());
//            }
//        });

        //设置已经签到的日期
        widget.addDecorator(new PrimeDayDisableDecorator());
    }

    //不可选名单
    private class PrimeDayDisableDecorator implements DayViewDecorator {

        private boolean[] PRIME_TABLE = {
                false,  // 0
                false,
                true, // 2
                true, // 3
                false,
                true, // 5
                false,
                false, // 7
                false,
                false,
                false,
                true, // 11
                false,
                true, // 13
                false,
                false,
                false,
                true, // 17
                false,
                true, // 19
                false,
                false,
                false,
                true, // 23
                false,
                false,
                false,
                false,
                false,
                true, // 29
                false,
                true, // 31
                false,
                false,
                false, //PADDING
        };

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return PRIME_TABLE[day.getDay()];
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.setDaysDisabled(true);
            //斜体
//            view.addSpan(new StyleSpan(Typeface.ITALIC));
            //字体大小
//            view.addSpan(new RelativeSizeSpan(1.0f));
            //字体颜色
//            view.addSpan(new ForegroundColorSpan(Color.rgb(17,182,244)));
            //小点
//            view.addSpan(new DotSpan(10, Color.RED));
            //图片资源
            Resources resources = Activity_Punch_Sign.this.getResources();
            Drawable drawable1 = resources.getDrawable(R.drawable.sign_ok);
//            view.setSelectionDrawable(drawable1);
            view.setBackgroundDrawable(drawable1);
        }
    }
}
